﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimControl : MonoBehaviour {

    [HideInInspector]
   public  Animation anim;
    AnimationState clipState;
    string[] animName;
    string FinishName = "CallFinish";
    string BeginName = "CallBegin";
    System.Action<string> OnFinish;
    System.Action OnFinishOnce;
    System.Action<string> OnBegin;
    System.Action OnBeginOnce;
     bool isReverse;
   
    
    private void Awake()
    {
        anim = GetComponent<Animation>();
        animName = new string[anim.GetClipCount()];
        int i = 0;
        foreach (AnimationState state in anim)
        {
            animName[i++] = state.name;
            //给clip的最后一帧注册事件
            if(state.wrapMode!=WrapMode.Loop)
            AddEvent(state.length, FinishName, state.name);
         //    AddEvent(0, BeginName, state.name);
        }   
    }

   
    /// <summary>
    /// 给某一帧注册事件
    /// </summary>
    /// <param name="EventTime"></param>
    /// <param name="funcname"></param>
    /// <param name="name"></param>
    void AddEvent(float EventTime, string funcname, string name)
    {
        AnimationEvent auidoEvent = new AnimationEvent
        {
            time = EventTime,
            functionName = funcname,
            stringParameter = name

        };
        anim.GetClip(name).AddEvent(auidoEvent);
      
    }


     void CallFinish(string name)
    {
        if (isReverse)
        {
       
            if (OnBeginOnce != null)
                OnBeginOnce();
        }
        else
        {
           
            if (OnFinishOnce != null)
                OnFinishOnce();
        }
     
    }

    void CallBegin(string name)
     {

        if (!isReverse)
        {
            if (OnBeginOnce != null)
                OnBeginOnce();
        }
        else
        {
            if (OnFinishOnce != null)
                OnFinishOnce();
        }
    }




    public void Reset()
    {
        anim.Stop();
       PlayComplete();
     

    }
    /// <summary>
    /// 播放animation
    /// </summary>
    /// <param name="name">cilp的名字</param>
    /// <param name="onfinish">动画完成事件</param>
    public void Play(string name="",System.Action onfinish=null)
    {

        isReverse = false;
        if (string.IsNullOrEmpty(name))
        {
            name = anim.clip.name;
        }
        clipState = anim[name];
   
        if (clipState != null)
        {
            clipState.speed = 1f;
       
            anim.Play(name);

        }
        OnFinishOnce = onfinish;
    }
    /// <summary>
    /// 从头播放animation
    /// </summary>
    /// <param name="name">动画clip的名字</param>
    /// <param name="onfinish">动画完成事件</param>
    public void PlayComplete(string name="", System.Action onfinish = null)
    {
        isReverse = false;
        if (string.IsNullOrEmpty(name))
        {
            name = anim.clip.name;
        }
        clipState = anim[name];
        if (clipState != null)
        {
            clipState.speed = 1f;
            clipState.time = 0;
            anim.Play(name);
          
        }
        OnFinishOnce = onfinish;
    }
    /// <summary>
    /// 从尾巴开始倒播animation
    /// </summary>
    /// <param name="name">动画clip的名字</param>
    /// <param name="onfinish">动画完成事件</param>
    public void PlayReverseComplete(string name = "", System.Action onfinish = null)
    {
        isReverse = true;
        if (string.IsNullOrEmpty(name))
        {
            name = anim.clip.name;
        }
        clipState = anim[name];
        if (clipState != null)
        {
            clipState.speed = -1f;
            clipState.time = clipState.length;
            anim.Play(name);
        }
        OnBeginOnce = onfinish;
    }



    /// <summary>
    /// 倒播animation
    /// </summary>
    /// <param name="name">动画clip的名字</param>
    /// <param name="onfinish">动画完成事件</param>
    public void PlayReverse(string name = "", System.Action onfinish = null, float speed = -1)
    {
        isReverse = true;
        if (string.IsNullOrEmpty(name))
        {
            name = anim.clip.name;
        }
        clipState = anim[name];
        if (clipState != null)
        {
            clipState.speed = speed;
            
            anim.Play(name);
        }
        OnBeginOnce = onfinish;
    }
    /// <summary>
    /// 根据index播放
    /// </summary>
    /// <param name="index">clip 数组的下标</param>
    /// <param name="onfinish">播放完成事件</param>
    public void Play(int index, System.Action onfinish = null)
    {
        if (index < animName.Length)
            Play(animName[index],onfinish);
    }
    /// <summary>
    /// 根据index倒播
    /// </summary>
    /// <param name="index"></param>
    /// <param name="onfinish"></param>
    public void PlayReverse(int index, System.Action onfinish = null)
    {
        if (index < animName.Length)
            PlayReverse(animName[index],onfinish);
    }

    /// <summary>
    /// 停止播放动画
    /// </summary>
    public void Stop()
    {
        anim.Stop();
    }

    public void Update()
    {
        //if (Input.GetKey(KeyCode.Q))
        //{
        //    PlayReverse(1);
        //}
        //if (Input.GetKey(KeyCode.W))
        //{
        //    Play(1);
        //}
    }
}
